interneurons <- readRDS("../../data/interneurons_kb.rds")
DefaultAssay(interneurons) <- "RNA"

interneurons = FindVariableFeatures(interneurons, selection.method = "vst", nfeatures = 10000)
all.genes = rownames(interneurons)
interneurons = ScaleData(interneurons)

interneurons = RunPCA(interneurons, features = VariableFeatures(object = interneurons))


interneurons = RunUMAP(interneurons, reduction = "pca", dims = 1:30)
interneurons= FindNeighbors(interneurons, reduction = "pca", dims = 1:30)


interneurons = FindClusters(interneurons, resolution = .9)
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 19554
## Number of edges: 651886
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.8345
## Number of communities: 23
## Elapsed time: 2 seconds
DimPlot(interneurons, reduction = "umap", label = TRUE, repel = TRUE,pt.size = 1)

plot_colors = read_delim("../../data/palettes/interneuron_timepoint_pal.txt",
                         delim = "/n",col_names = F) %>% pull(X1)

time_points <- unique(interneurons@meta.data["timepoint"]) %>% pull(timepoint) 

col_n <-set_names(plot_colors,levels(time_points))

#,order = rev(levels(time_points))
DimPlot(interneurons,
        reduction = "umap",
        repel = TRUE,
        group.by = "timepoint",
        pt.size = 2,
        cols = col_n,order = rev(levels(time_points)))+
  ggtitle(label = "")+ theme(legend.position = "none")

pre.critical <- levels(time_points)[1:5]

# Add new timepoint column for pre and post critical period
interneurons@meta.data <- interneurons@meta.data %>%
  mutate(timepoint2 = ifelse(timepoint %in% pre.critical,
                             "E14-P5",
                             "P7-Adult"))


DimPlot(interneurons, reduction = "umap", label = TRUE, repel = TRUE,pt.size = 1, group.by =  "timepoint2")

DimPlot(interneurons, reduction = "umap", label = TRUE, repel = TRUE, split.by =  "timepoint2",pt.size = 1)

saveRDS(interneurons,"../../data/interneurons_labeled.RDS")
DefaultAssay(interneurons) <- "RNA"

all.markers <- FindAllMarkers(interneurons,logfc.threshold = 0.25)

sig.markers <- all.markers %>%
  filter(p_val_adj < 0.05) 



write_csv(sig.markers, file = "sig_markers_1.csv")
table(Idents(interneurons)) %>% kbl(col.names = c("Cluster","Cell Count")) %>% kable_minimal()
Cluster Cell Count
0 2214
1 1895
2 1433
3 1320
4 1268
5 1236
6 1219
7 1213
8 1179
9 1115
10 795
11 724
12 708
13 684
14 648
15 518
16 390
17 328
18 291
19 197
20 109
21 42
22 28
DimHeatmap(interneurons, dims = 1:15, cells = 500, balanced = TRUE)

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1)

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1,dims=c(2,3))

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1,dims=c(3,4))

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1,dims=c(4,5))

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1,dims=c(5,6))

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1,dims=c(5,7))

DimPlot(interneurons, reduction = "pca", group.by = "timepoint",pt.size = 1,dims=c(7,8))

interneurons[["percent.olf"]] <- PercentageFeatureSet(interneurons, pattern = "^Olfr")
interneurons[["percent.vmn"]] <- PercentageFeatureSet(interneurons, pattern = "^Vmn")

FeaturePlot(interneurons,features = "percent.olf",label = TRUE, repel = TRUE, min.cutoff = "q10", max.cutoff = "q90",pt.size = 1)

FeaturePlot(interneurons,features = "percent.vmn",label = TRUE, repel = TRUE, min.cutoff = "q10", max.cutoff = "q90",pt.size = 1)